package com.mall.mall.coupon.service.impl;

import com.mall.mall.coupon.entity.SeckillSkuRelationEntity;
import com.mall.mall.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.Query;

import com.mall.mall.coupon.dao.SeckillSessionDao;
import com.mall.mall.coupon.entity.SeckillSessionEntity;
import com.mall.mall.coupon.service.SeckillSessionService;
import org.springframework.util.CollectionUtils;

import javax.xml.crypto.Data;

import static java.time.Duration.ofDays;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getLasted3DaysSession() {
        // 计算最近三天的时间
        Date date = new Date();
        LocalDate now = LocalDate.now();
        LocalDate plus = now.plusDays(1);
        LocalDate plus1 = now.plusDays(2);

        LocalTime min = LocalTime.MIN;
        LocalTime max = LocalTime.MAX;

        LocalDateTime startTime = LocalDateTime.of(now, min);
        startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime endTime = LocalDateTime.of(plus1, max);
        endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        List<SeckillSessionEntity> seckillSessionEntityList = this.list(new QueryWrapper<SeckillSessionEntity>().between("start_time", startTime, endTime));

        if (!CollectionUtils.isEmpty(seckillSessionEntityList)){
            List<SeckillSessionEntity> seckillSessionEntities = seckillSessionEntityList.stream().map(session -> {
                Long id = session.getId();
                List<SeckillSkuRelationEntity> promotion_session_id = seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", id));
                session.setRelationSkus(promotion_session_id);
                return session;
            }).collect(Collectors.toList());
            return seckillSessionEntities;
        }

        return null;
    }

}